use eframe::egui::{self, FontFamily, FontId, TextStyle};

pub fn configure_text_styles(ctx: &egui::Context) {
    use FontFamily::Proportional;

    let mut style = (*ctx.style()).clone();
    style.text_styles = [
        (TextStyle::Heading, FontId::new(18.0, Proportional)),
        (TextStyle::Body, FontId::new(16.0, Proportional)),
        (TextStyle::Monospace, FontId::new(16.0, Proportional)),
        (TextStyle::Button, FontId::new(16.0, Proportional)),
        (TextStyle::Small, FontId::new(14.0, Proportional)),
    ]
    .into();
    ctx.set_style(style);
}

pub fn setup_custom_fonts(ctx: &egui::Context) {
    // start with the default fonts (we will be adding to them rather than replacing them).
    let mut fonts = egui::FontDefinitions::default();

    //// .ttf and .otf files supported.
    //fonts.font_data.insert(
    //    "nerd_font".to_owned(),
    //    egui::FontData::from_static(resource::NERD_FONT),
    //);

    //// put my font first (highest priority) for proportional text:
    //fonts
    //    .families
    //    .entry(egui::FontFamily::Proportional)
    //    .or_default()
    //    .insert(0, "nerd_font".to_owned());

    //// put my font as last fallback for monospace:
    //fonts
    //    .families
    //    .entry(egui::FontFamily::Monospace)
    //    .or_default()
    //    .insert(0, "nerd_font".to_owned());

    // chinese character on window
    #[cfg(target_os = "windows")]
    {
        let font = std::fs::read("c:/Windows/Fonts/msyh.ttc").unwrap();
        fonts.font_data.insert(
            "microsoft_yahei".to_owned(),
            egui::FontData::from_owned(font),
        );

        fonts
            .families
            .entry(egui::FontFamily::Proportional)
            .or_default()
            .push("microsoft_yahei".to_owned());

        fonts
            .families
            .entry(egui::FontFamily::Monospace)
            .or_default()
            .push("microsoft_yahei".to_owned());
    }

    // chinese character on macos
    #[cfg(target_os = "macos")]
    {
        let font = std::fs::read("/System/Library/Fonts/PingFang.ttc").unwrap();
        fonts
            .font_data
            .insert("PingFang".to_owned(), egui::FontData::from_owned(font));

        fonts
            .families
            .entry(egui::FontFamily::Proportional)
            .or_default()
            .push("PingFang".to_owned());

        fonts
            .families
            .entry(egui::FontFamily::Monospace)
            .or_default()
            .push("PingFang".to_owned());
    }

    // tell egui to use these fonts:
    ctx.set_fonts(fonts);
}